<template>
  <el-container>
    <el-main class="main-bg">
      <el-form ref="searchForm" :model="searchForm" label-width="80px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="物品名称">
              <el-input
                v-model="searchForm.goodName"
                placeholder="请输入物品名称"
              />
            </el-form-item>
          </el-col>
          <el-col :offset="12" :span="6" class="text-right">
            <el-button
              icon="iconfont iconxianxing_sousuo"
              type="primary"
              @click="handleSearch"
            >
              查询
            </el-button>
            <el-button
              icon="iconfont iconxianxing_zhongzhi"
              @click="handleReset"
            >
              重置
            </el-button>
          </el-col>
        </el-row>
      </el-form>
      <Table
        ref="table"
        :current-page="page"
        :header-data="tableHeader"
        :index="false"
        :loading="loading"
        :page-size="limit"
        :table-data="tableData"
        :total="total"
        class="data-table"
        style="width: 100%"
        @now-page="handlePage"
        @now-size="handleSize"
        @table-sort="handleSort"
      />
    </el-main>
  </el-container>
</template>

<script>
import addTable from '@/mixins/addtable'
import { getGoodList } from './api'

export default {
  name: 'GoodList',
  mixins: [addTable],
  data() {
    return {
      searchForm: {
        goodName: ''
      },
      tableHeader: [],
    }
  },
  created() {
    this.tableHeader = [
      {
        prop: 'goodName',
        label: '物品名称',
        sortable: false,
        overflow: true,
        align: 'left'
      },
      {
        prop: 'goodNum',
        label: '库存数量',
        sortable: false,
        overflow: true,
        align: 'left'
      },
      {
        prop: 'goodUnitName',
        label: '计量单位',
        sortable: false,
        overflow: true,
        align: 'left'
      },
      {
        prop: 'goodType',
        label: '规格',
        sortable: false,
        overflow: true,
        align: 'left'
      }
    ]
    this.getTableData()
  },

  methods: {
    getTableData() {
      this.loading = true
      this.getTableParams()
      getGoodList(this.tableParams)
          .then((res) => {
            if (!res) return
            const { data, pageInfo } = res
            setTimeout(() => {
              this.loading = false
              this.tableData = data
              this.total = pageInfo.total
            }, 500)
          })
          .catch((err) => {
            console.log('TCL: getTableData -> err', err)
          })
    },
  }
}
</script>
